package leetcode.每日一题;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @author :songyaozhang
 * @date :Created 2021/8/10 11:03
 * @description :https://leetcode-cn.com/problems/arithmetic-slices/
 */
public class 等差数列划分 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.numberOfArithmeticSlices(new int[]{1,2,3,4,5}));
    }
    /*
    class Solution {
        public int numberOfArithmeticSlices(int[] nums) {
            if (nums.length < 3) return 0;
            int count = 0;
            for (int i = 0; i < nums.length - 2; i++) {
                int j = i + 1;
                    int diff = nums[j] - nums[i];
                    int third = nums[j] + diff;
                    if (nums[j + 1] == third) {
                        int end = j + 1;
                        while (end < nums.length - 1 && nums[end + 1] == third + diff) {
                            third += diff;
                            end++;
                        }
                        System.out.println(j + 1 + " " + end);
                        count += end - j;
                    }
            }
            return count;
        }
    }
     */
    class Solution {
        public int numberOfArithmeticSlices(int[] nums) {
            int n = nums.length;
            if (n < 3) return 0;
            int ans = 0;
            int d = nums[0] - nums[1];
            int t = 0;
            for (int i = 2; i < n; i++) {
                if (nums[i - 1] - nums[i] == d) {
                    t++;
                } else {
                    d = nums[i - 1] - nums[i];
                    t = 0;
                }
                ans += t;
            }
            return ans;
        }
    }
}
